<?php
/**
 * Created by PhpStorm.
 * User: jurentech
 * Date: 2019-06-17
 * Time: 22:35
 */
namespace app\substructure\controller;

class Xinxi extends Base
{
    public function index()
    {
        $son_id=session('son_id');
        $count['all'] = db('order')->where(['status'=>1,'son_id'=>$son_id])->count();
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $count['day'] = db('order')->where(['son_id'=>$son_id,'status'=>1,'create_time'=>['between',[$beginToday,$endToday]]])->count();
        $count['son']=db('son')->where(['son_id'=>$son_id])->find();
        $this->assign('shuliang',$count);
        return $this->fetch();
    }

    public function chuli()
    {
        return $this->fetch();
    }

    public function sender()
    {
        if(request()->isPost()){
            $data['order_id']=input('post.order_id');
            $data['content']=input('post.content');
            $data['create_time']=time();
            db('order')->where(['order_id'=>$data['order_id']])->update(['status'=>2]);
            db('reorder')->insert($data);
            return $this->_ajax_return(200,'成功',"index");
        }
        $id=input('id');
        $order=db('order')->where(['order_id'=>$id])->find();
        $this->assign('order',$order);
        return $this->fetch();
    }

    public function getorder()
    {
        $son_id=session('son_id');

        $page['page'] = input("page");
        $page['limit'] = input("limit");
        $result['data'] = db('order')
            ->where(['son_id'=>$son_id,'status'=>1])
            ->order('create_time DESC')
            ->page($page['page'], $page['limit'])
            ->select();
        foreach ($result['data'] as $v=>$k){
            $result['data'][$v]['area_name']=db('area')->where(['area_id'=>$result['data'][$v]['area_id']])->value('area_name');
            $result['data'][$v]['create_time'] = date('Y-m-d H:i:s', $result['data'][$v]['create_time']);
            $result['data'][$v]['son_name']=db('son')->where(['son_id'=>$result['data'][$v]['son_id']])->value('son_name');
        }
        $result['count'] = db('order')->where(['son_id'=>$son_id,'status'=>1])->count();
        return $this->_table_return(0, "", $result['count'], $result['data']);
    }

    public function getreorder()
    {
//        ->alias("h")
//        ->field("x.username as hexiao_name, o.username as organ_name, FROM_UNIXTIME(h.create_time) as create_time")
//        ->join("hexiao x", "h.hexiao_id = x.admin_id")
//        ->join("organ o", "o.admin_id = x.organ_id")
//        ->where("h.order_id", $id)
//        ->order("h.hexiaojilu_id", "DESC")
//        ->select();
        $son_id=session('son_id');
        $page['page'] = input("page");
        $page['limit'] = input("limit");
        $result['data'] = db('reorder')
            ->alias("r")
            ->field("o.order_id,r.create_time,r.status,o.name,o.phone")
            ->join("order o", "r.order_id = o.order_id")
            ->where(['o.son_id'=>$son_id])
            ->order('r.create_time DESC')
            ->page($page['page'], $page['limit'])
            ->select();
        foreach ($result['data'] as $v=>$k){
            $result['data'][$v]['create_time'] = date('Y-m-d H:i:s', $result['data'][$v]['create_time']);
        }
        $result['count'] = db('reorder')->alias("r")->join("order o", "r.order_id = o.order_id")->where(['o.son_id'=>$son_id])->count();
        return $this->_table_return(0, "", $result['count'], $result['data']);
    }

    public function change()
    {
        if(request()->isPost()){
            $order_id=input('post.order_id');
            $son_id=input('post.son_id');
            $order=db('order')->where(['order_id'=>$order_id])->find();
            $order['son_id']=$son_id;
            $order['create_time']=time();
            unset($order['order_id']);
            db('order')->insert($order);
            $count=db('son')->where(['son_id'=>$son_id])->value('order_count');
            $count=$count+1;
            db('son')->where(['son_id'=>$son_id])->update(['order_count'=>$count]);
            $this->_ajax_return(200,'成功','index');

        }
        $id=input('id');
        $order=db('order')->where(['order_id'=>$id])->find();
        $list=db('son')->where(['area_id'=>$order['area_id']])->select();
        $this->assign('order',$order);
        $this->assign('list',$list);
        return $this->fetch();
    }
}